import os
import xml.dom.minidom
import cv2


def write_xml(info, W, H):
    FileName = info[0].split("\\")[-1]
    xml_file = open((info[0]), 'w')
    xml_file.write('<annotation>\n')
    xml_file.write('    <folder>VOC2007</folder>\n')
    xml_file.write('    <filename>' + FileName + '</filename>\n')
    xml_file.write('    <size>\n')
    xml_file.write('        <width>' + str(W) + '</width>\n')
    xml_file.write('        <height>' + str(H) + '</height>\n')
    xml_file.write('        <depth>3</depth>\n')
    xml_file.write('    </size>\n')
    for img_each_label in info[1:]:
        spt = img_each_label[4]
        xml_file.write('    <object>\n')
        xml_file.write('        <name>' + spt + '</name>\n')
        xml_file.write('        <pose>Unspecified</pose>\n')
        xml_file.write('        <truncated>0</truncated>\n')
        xml_file.write('        <difficult>0</difficult>\n')
        xml_file.write('        <bndbox>\n')
        xml_file.write('            <xmin>' + str(img_each_label[0]) + '</xmin>\n')
        xml_file.write('            <ymin>' + str(img_each_label[1]) + '</ymin>\n')
        xml_file.write('            <xmax>' + str(img_each_label[2]) + '</xmax>\n')
        xml_file.write('            <ymax>' + str(img_each_label[3]) + '</ymax>\n')
        xml_file.write('        </bndbox>\n')
        xml_file.write('    </object>\n')

    xml_file.write('</annotation>')


def xml_read(filename):
    # print(filename)
    if os.path.exists(filename) is False:
        return None
    dom = xml.dom.minidom.parse(filename)
    # 得到文档元素对象
    root = dom.documentElement

    object_ = root.getElementsByTagName('object')
    info = []
    # filename = root.getElementsByTagName('filename')[0].firstChild.data
    for object_1 in object_:
        name = object_1.getElementsByTagName("name")[0].firstChild.data
        bndbox = object_1.getElementsByTagName("bndbox")[0]
        xmin = int(bndbox.getElementsByTagName("xmin")[0].firstChild.data)
        ymin = int(bndbox.getElementsByTagName("ymin")[0].firstChild.data)
        xmax = int(bndbox.getElementsByTagName("xmax")[0].firstChild.data)
        ymax = int(bndbox.getElementsByTagName("ymax")[0].firstChild.data)
        # if name=='40\'':
        #   print(filename,xmin,ymin,xmax,ymax)
        # if 'limitRate'==name or 'limit'==name:
        info.append([xmin, ymin, xmax, ymax, name])
    return info


# ======================================================================函数功能
# 将最大框外的边缘缩减一半

# ======================================================================参数设置begin
SrcDir = r'F:\403\ImgOutput\Video2Jpg'  # 源文件路径

# ======================================================================参数设置end

names = []


for files in os.scandir(SrcDir):
    if files.name[-1] == 'l':
        names.append(files.name)
LenNames = len(names)

for file_name in names:
    img = cv2.imread(SrcDir + "\\" + file_name[:-4] + '.jpg')
    i_h, i_w, _ = img.shape
    locations = xml_read(SrcDir + "\\" + file_name)
    # NewLocations = [DstDir + "\\" + file_name]

    NewLocations = [SrcDir + "\\" + file_name]
    for location in locations:
        NewLocations.append(location)
    write_xml(NewLocations, i_w, i_h)

print("%d files are done")
